package se.webbzon.boltzmann.minigame.sandbox;

import java.awt.Image;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceIterator;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SandBoxPicture extends DefaultSandBoxItem implements SandBoxAnimatable {
	
	// The animation of this sand box picture
	private SandBoxAnimation animation;
	
	// The resource iterator
	private ResourceIterator<?> it;
	
	// The horizontal and vertical repeating of the picture
	private int hRep, vRep;
	
	// Whatever this picture is visible or not
	private boolean visible;
	
	// The transparency value of this picture
	private float alpha;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SandBoxPicture(ResourceSet<?> pictureSet, int depth2d) {
		setPicture(pictureSet);
		setInstance(new Instance(depth2d));
		setSize(32,32);
		hRep = 1;
		vRep = 1;
		visible = true;
		alpha = 1;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the horizontal repeating of this sand box picture. **/
	public int getHRep() {
		return hRep;
	}

	/** Sets the horizontal repeating of this sand box picture. **/
	public void setHRep(int hRep) {
		this.hRep = hRep;
	}

	/** Returns the vertical repeating of this sand box picture. **/
	public int getVRep() {
		return vRep;
	}

	/** Returns the vertical repeating of this sand box picture. **/
	public void setVRep(int vRep) {
		this.vRep = vRep;
	}
	
	/** Returns true if this sand box picture is visible. **/
	public boolean isVisible() {
		return visible;
	}

	/** Sets whatever this sand box picture is visible. **/
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	/** Returns the iteration speed of this sand box picture. **/
	public double getSpeed() {
		return it.getSpeed();
	}
	
	/** Sets the iteration speed of this sand box picture. **/
	public void setSpeed(double speed) {
		it.setSpeed(speed);
	}
	
	/** Returns the iteration index of this sand box picture. **/
	public double getIndex() {
		return it.getIndex();
	}
	
	/** Sets the iteration index of this sand box picture. **/
	public void setIndex(double index) {
		it.setIndex(index);
	}

	/** Sets the image/texture set of this sand box picture. **/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setPicture(ResourceSet<?> pictureSet) {
		if (pictureSet == null)
			pictureSet = Resources.getImageSet("minigame_sandbox_container");
		Object o = pictureSet.get(0);
		if (!(o instanceof Image) && !(o instanceof Texture3D)) 
			pictureSet = Resources.getImageSet("minigame_sandbox_container");
			
		it = new ResourceIterator(pictureSet);
		it.setSpeed(1);
	}
	
	@Override public SandBoxAnimation getAnimation() {
		return animation;
	}
	
	@Override public void setAnimation(SandBoxAnimation animation) {
		this.animation = animation;
	}
	
	@Override public void setSize(double w, double h) {
		Instance i = (Instance) getInstance();
		i.setSize(w, h);
	}

	@Override public float getTransparency() {
		return alpha;
	}
	
	@Override public void setTransparency(float transparency) {
		alpha = transparency;
	}
	
	@Override public void setRotation(double rotation) {};

	@Override public double getRotation() {
		return 0;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends SandBoxObject {

		public Instance(int depth2d) {
			super(WorldObject.onPostDraw + WorldObject.onStepEnd, depth2d);
		}
		
		public void setSize(double w, double h) {
			super.setSize(w, h);
		}

		@Override public void onAttach(SandBox box) {};

		@Override public void onDetach(SandBox box) {};

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onStepEnd(WorldArea world) {
			if (animation != null)
				animation.onStepEnd(SandBoxPicture.this);
			if (it != null)
				it.next();
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			if (!visible)
				return;
			
			int w = (int) getWidth();
			int h = (int) getHeight();
			int dx = w/hRep;
			int dy = h/vRep;
			
			Object current = it.current();
			Image img;
			if (current instanceof Image)
				img = (Image) current;
			else
				img = ((Texture3D) current).getImage();
			
			if (alpha != 1)
				hud.setTransparency(alpha);
			
			int x = (int) getX() - w/2;
			int y = (int) getY() - h/2;
			for (int i = 0; i < hRep; i++) {
				for (int j = 0; j < vRep; j++) {
					hud.drawImage(img, x + dx * i, y + dy * j, dx, dy);
				}
			}
			
			if (alpha != 1)
				hud.setTransparency(1);
		}
		
	}

}
